package mx.tools;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

public class MapperJSON {

	public Map string2Map(String mesgtx) throws Exception {
		return parseJSON2Map(mesgtx);
	}
	
	public String map2String(Map map) throws Exception {
		return buildMap2JSON(map);
	}
	
	public String map2Pretty(Map map) throws Exception {
		return buildMap2JSON(map);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		System.out.println("------------------");
		String jsonContent = "{ abc : [{'hello':333.01,'abc':'中国人12三','xyz':{'a':1,'b':'ab'}},{'hello':555,'abc':'true','xyz':{'a':2,'b':'ba'}}] }";
		Map m = parseJSON2Map(jsonContent);
		System.out.println(m);
		
		String x = buildMap2JSON(m);
		System.out.println(x);
	}

	public static Object JSON2Object(Object json) throws Exception {
		if (json instanceof JSONArray) {
            List<Object> list = new ArrayList<Object>();
            JSONArray jsonArray = (JSONArray) json;
            for(int i=0; i<jsonArray.length(); i++) {
                Object json2 = jsonArray.get(i);
                Object value = JSON2Object(json2);
                list.add(value);  
            }
            return list;
		}
		
		if (json instanceof JSONObject) {
			Map<String, Object> map = new LinkedHashMap<String, Object>();
            JSONObject jsonObject = (JSONObject) json;
            Iterator<String> iterator = jsonObject.keys(); 
            while ( iterator.hasNext() ) {
            	String key = iterator.next();
            	Object json2 = jsonObject.get(key);
            	Object value = JSON2Object(json2);
            	map.put( key,  value);
            }
            return map;
		}

		if (json instanceof Double) {
			return new BigDecimal(json.toString());
		}
		
		return json;
	}
	
	public static Object Object2JSON(Object obj) throws Exception {
		if (obj instanceof List) {
			JSONArray jsonArray = new JSONArray();
			List list = (List) obj;
			for (int i=0; i<list.size(); i++) {
				Object obj2 = list.get(i);
				Object value = Object2JSON(obj2);
				jsonArray.put(value);
			}
			return jsonArray;
		}
		
		if (obj instanceof Map) {
			JSONObject jsonObject = new JSONObject();
			Map<String, Object> map = (Map<String,Object>) obj;
			for (String key : map.keySet()) {
				Object obj2 = map.get(key);
				Object value = Object2JSON(obj2);
				jsonObject.put(key, value);
			}
			return jsonObject;
		}
		
		return obj;
	}
	
	
	public static Map<String,Object> parseJSON2Map(String jsonStr) throws Exception {  
		JSONObject json = new JSONObject(jsonStr);  
		return (Map) JSON2Object(json);
	}
	
	public static String buildMap2JSON(Map map) throws Exception {
		JSONObject json = (JSONObject) Object2JSON(map);
		return json.toString();
	}
	
}
